# from django.shortcuts import render

# # Create your views here.
# from django.http import HttpResponse
# """
#  django.http模块中定义了HttpResponse 对象的API
#  作用：不需要调用模板直接返回数据
#  HttpResponse属性：
#     content: 返回内容,字符串类型
#     charset: 响应的编码字符集
#     status_code: HTTP响应的状态码
# """
#
# """
# hello 为一个视图函数，每个视图函数必须第一个参数为request。哪怕用不到request。
# request是django.http.HttpRequest的一个实例
# """
# def hello(request):
#     return HttpResponse('Hello World')

# from django.shortcuts import render
# def hello(request):
#     context = {}
#     context['hello'] = 'Hello World!'
#     return render(request, "app_demo/helloworld.html", context)

# def hello(request):
#     views_name = "qaq"
#     return render(request, "app_demo/helloworld.html", {"name":views_name})

# def hello(request):
#     views_list = ["菜鸟教程1","菜鸟教程2","菜鸟教程3"]
#     return render(request, "app_demo/helloworld.html", {"views_list": views_list})

# def hello(request):
#     views_dict = {"name":"Learning makes me happy","name1":"Learning makes me happy"}
#     return render(request, "app_demo/helloworld.html", {"views_dict": views_dict})
# def hello(request):
#     name = "Learning makes me happy"
#     return render(request, "app_demo/helloworld.html", {"name": name})
# def hello(request):
#     import datetime
#     now = datetime.datetime.now()
#     return render(request, "app_demo/helloworld.html", {"time": now})
# def hello(request):
#     views_str = "Learning makes me happy"
#     return render(request, "app_demo/helloworld.html", {"views_str": views_str})

# def hello(request):
#     views_str = "<a href='https://blog.gobgm.com/'>Click the jump</a>"
#     return render(request, "app_demo/helloworld.html", {"views_str": views_str})
# def hello(request):
#     views_num = 88
#     return render(request, "app_demo/helloworld.html", {"num": views_num})
#
# def hello(request):
#     views_list = [10, "Learning makes me happy", 30, 40, ]
#     return render(request, "app_demo/helloworld.html", {"views_list":views_list})
# def hello(request):
#     views_dict = {"name":"px","motto":"Learning makes me happy"}
#     return render(request, "app_demo/helloworld.html", {"views_dict":views_dict})
#
# def hello(request):
#     views_list = ["a", "b", "c", "d", "e"]
#     return render(request, "app_demo/helloworld.html", {"listvar":views_list})

# def hello(request):
#     views_list = []
#     return render(request, "app_demo/helloworld.html", {"listvar":views_list})

from django.shortcuts import render


def hello(request):
    name = "Learning makes me happy"
    return render(request, "app_demo/helloworld.html", {"name": name})


# def login(request):
#     return render(request,"app_demo/login.html")
#     ret = reverse("app_demo:login")
#     print(ret)
#     return render(request, "app_demo/login.html")
def timer(request):
    import time
    ctime = time.strftime("%Y-%m-%d %X", time.localtime())
    return render(request, "timer.html", {"date": ctime})


def special_2003(request):
    return HttpResponse("2003")


def year(request, y):
    return HttpResponse(y)


def month(request, year, month):
    return HttpResponse(year + "-" + month)


# from django.shortcuts import render,HttpResponse
# from django.urls import reverse
# # Create your views here.
# def login(request):
#     # 校验用户名密码,得从post请求中,获取到用户输入的user和pwd,所有的请求信息都在request中
#     #1.获取请求方式是GET还是POST
#     print(reverse("app_demo:LOGIN"))
#     print(request.method)
#     if request.method=="GET":
#     # render的时候,会渲染login.html,当遇到模板语法{}的时候,会到urls中找相应的别名,然后取出路径,渲染到login.html中.再返回给浏览器
#         return render(request, "app_demo/login.html")
#     else:
#         # 2.post请求信息在request.POST中,这是一个字典形式的数据
#         print(request.POST)
#         user = request.POST.get("user")
#         pwd = request.POST.get("pwd")
#         if user =="admin" and pwd=="admin":
#             return  HttpResponse("登录成功!")
#         else:
#             return  HttpResponse("登录失败,请检查用户名密码!")


from django.shortcuts import render
from django.shortcuts import reverse  # 用于反向解析
from django.shortcuts import redirect  # 用于重定向页面
from django.shortcuts import HttpResponse


# from django.http import HttpResponse, HttpResponseRedirect
def login(request):
    if request.method == "GET":
        # 当为get请求时，返回login.html页面,页面中的{% url 'app_demo:login' %}会被反向解析成路径：/login1
        return render(request, "app_demo/login.html")
    else:
        # 当为post请求时，可以从request.POST中取出请求体的数据
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        if username == "admin" and pwd == "admin":
            # 第一种跳转方式
            index1url = reverse('app_demo:new_page')
            return redirect(index1url)  # reverse会将别名'new_page'反向解析成路径：/new/
            # 第二种跳转方式
            # return HttpResponseRedirect(reverse('app_demo:new_page'))  # reverse会将别名'new_page'反向解析成路径：/new/
            # 这个是直接返回文本
            # return HttpResponse('login succeed')
        else:
            # return redirect("/add_emp/")
            return HttpResponse('Learning makes me happy---->authentication failed！！！！！')


def index1(request):
    return render(request, 'app_demo/index.html')


from django.shortcuts import HttpResponseRedirect


def old(request):
    # return HttpResponse('这是一个old_page')
    return HttpResponseRedirect(reverse('app_demo:new_page'))
    # return redirect(reverse('app_demo:new_page'))

from django.utils.translation import gettext as _
def new(request):
    output = _("这是一个new_page")
    return HttpResponse(output)
    # return render(request, 'app_demo/index.html')


# index 的 views.py
# from django.views.decorators.cache import cache_page
# 参数cache与全站缓存CACHE_MIDDLEWARE_ALIAS相同
# 参数key_prefix与全站缓存CACHE_MIDDLEWARE_KEY_PREFIX相同
# @cache_page(timeout=10, cache='default', key_prefix='mysiteindex')
def index(request):
    return HttpResponse('登录成功了，这是一个index_page')
    # return HttpResponse(reverse('app_demo:index'))
